package com.smrs.asn.webapp.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.smrs.asn.enums.AsnStatusEnum;
import com.smrs.asn.model.AsnHeaderModel;
import com.smrs.asn.model.AsnHeaderSearchModel;
import com.smrs.enums.ActionCommandEnum;
import com.smrs.security.model.UserModel;
import com.smrs.util.AppUtil;




public class AsnHeaderJsonAction extends AsnHeaderAction{
	private static final long serialVersionUID = 1L;
	
	public String searchAsnHeaderJson(){
		/*AsnHeaderSearchModel asnHeaderSearchModel = new AsnHeaderSearchModel();
		asnHeaderSearchModel.setAsnHeader(asnHeader);		
		asnHeaderSearchModel.setPager(pager);		
		pager=asnHeaderService.getByNameLikePager(asnHeaderSearchModel);
		*/
		//prepareList();
		//AsnHeaderSearchModel asnHeaderSearchModel = new AsnHeaderSearchModel();
		if(searchModel==null){
			searchModel = new AsnHeaderSearchModel();
		}
		ActionCommandEnum action = ActionCommandEnum.toEnum(actionCommand);
		if(action!=null && searchModel.getStatus()==null){
			switch(action){
				case audit:
				case unaudit:{					
					searchModel.setStatus(AsnStatusEnum.FRESH.getId());					
					break;
				}
				case account:
				case unaccount:{
					searchModel.setStatus(AsnStatusEnum.AUDITED.getId());
					break;
				}
				default:
					searchModel.setStatus(AsnStatusEnum.FRESH.getId());
			}
		}
		pager=asnHeaderService.findAsnHeader(searchModel,pager);
		//this.actionCommand="query";
		return SUCCESS;
	}
	
	public String updateAsnHeaderJson(){
		if(StringUtils.isEmpty(this.actionCommand)){
			//asnHeader = asnHeaderService.getByPK(asnHeader.getId());	
			//store = this.storeService.getByPK(asnHeader.getStoreId());
			//region = regionService.getByPK(asnHeader.getRegion().getId());
			this.addActionError("沒有输入操作方向,请输入操作方向");	
			return SUCCESS;
		}
		ActionCommandEnum action = ActionCommandEnum.toEnum(actionCommand);
		switch(action){
			case audit:{
				List<Long>  listIds = parseAsnHeaderIds(asnHeaderIds);
				for(Long asnId:listIds){
					AsnHeaderModel model = asnHeaderService.getByPK(asnId);
					model.setStatus(AsnStatusEnum.AUDITED.getId());
					UserModel userModel = this.getUserModelFromSession();
					AppUtil.setUpdateUserInfo(userModel, model);
					model.setAuditor(userModel.getName());
					model.setAuditTime(new Date());
					asnHeaderService.updateModel(model);				
				}			
				this.addActionMessage("修改成功");	
				break;
			}
			case unaudit:{
				List<Long>  listIds = parseAsnHeaderIds(asnHeaderIds);
				for(Long asnId:listIds){
					AsnHeaderModel model = asnHeaderService.getByPK(asnId);
					model.setStatus(AsnStatusEnum.FRESH.getId());				
					UserModel userModel = this.getUserModelFromSession();
					AppUtil.setUpdateUserInfo(userModel, model);
					model.setAuditor(userModel.getName());
					model.setAuditTime(new Date());
					asnHeaderService.updateModel(model);
				}			
				this.addActionMessage("修改成功");	
				break;
				}
			case account:{
				prepareAccountStatus();
				List<Long>  listIds = parseAsnHeaderIds(asnHeaderIds);
				for(Long asnId:listIds){
					AsnHeaderModel model = asnHeaderService.getByPK(asnId);
					model.setStatus(AsnStatusEnum.ACCOUNTED.getId());				
					UserModel userModel = this.getUserModelFromSession();
					AppUtil.setUpdateUserInfo(userModel, model);
					model.setAccountant(userModel.getName());
					model.setAccoutTime(new Date());
					asnHeaderService.updateModel(model);
					}			
				this.addActionMessage("修改成功");	
				break;
				}
			case unaccount:{
				prepareAccountStatus();
				List<Long>  listIds = parseAsnHeaderIds(asnHeaderIds);
				for(Long asnId:listIds){
					AsnHeaderModel model = asnHeaderService.getByPK(asnId);
					model.setStatus(AsnStatusEnum.AUDITED.getId());				
					UserModel userModel = this.getUserModelFromSession();
					AppUtil.setUpdateUserInfo(userModel, model);
					model.setAccountant(userModel.getName());
					model.setAccoutTime(new Date());
					asnHeaderService.updateModel(model);
					}			
				this.addActionMessage("修改成功");	
				break;
				}
			default:{
				this.addActionError("沒有输入操作方向,请输入操作方向");	
				}
		}
		return SUCCESS;
	}
	
}
